package com.killer_jin.core.common.util;

/**
 * Created by Frank Wu on 16/4/7.
 */
public class PathUtils {

    /**
     * 拼接多个字符串
     *
     * @param localSystemStyle 如果为false,则统一使用 /
     * @param paths
     * @return
     */
    public static String join(boolean localSystemStyle, String... paths) {
        String separator = (localSystemStyle && OsCheck.getOperatingSystemType() == OsCheck.OSType.Windows) ? "\\" : "/";

        StringBuilder builder = new StringBuilder();
        String prev = null;
        for (String path : paths) {
            if (path == null) {
                continue;
            }
            String finalPath = path;
            if (prev != null) {
                if (prev.endsWith(separator) && path.startsWith(separator)) {
                    finalPath = path.substring(1);
                } else if (!prev.endsWith(separator) && !path.startsWith(separator)) {
                    finalPath = separator + path;
                }
            }
            builder.append(finalPath);

            prev = finalPath;
        }

        return builder.toString();
    }

}
